package com.freecat.xmqgct.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;



public class DateUtils {

    private static SimpleDateFormat sf = null;

    public static String getCurrentDate() {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
        return sf.format(d);
    }

    public static String getCurrentDateStr() {
        Date d = new Date();
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(d);
    }

    public static String getCurrentDelOneDateStr(int i) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date beginDate = new Date();
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - i);
        Date endDate = null;
        try {
            endDate = dft.parse(dft.format(date.getTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dft.format(endDate);
    }

    public static boolean isDate2Bigger(String str1, String str2) {
        boolean isBigger = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt1 = null;
        Date dt2 = null;
        try {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (dt1.getTime() > dt2.getTime()) {
            isBigger = false;
        } else if (dt1.getTime() <= dt2.getTime()) {
            isBigger = true;
        }
        return isBigger;
    }

    /**
     * 时间戳转换string
     *
     * @param time
     * @return
     */


    public static String getStringTime(String time) {
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long l = Long.valueOf(time);
        String times = sf.format(new Date(l));//单位秒
        return times;
    }


    public static String getAge(String time) {
        SimpleDateFormat sf1 = new SimpleDateFormat("yyyy");
        String time1 = sf1.format(new Date());
        SimpleDateFormat sf2 = new SimpleDateFormat("yyyy");
        String times2 = sf2.format(new Date(Long.valueOf(time)));//单位秒
        String age = (Integer.parseInt(time1) - Integer.parseInt(times2)) + "";
        return age;
    }


    public static String getStringTimeSim(String time) {
        sf = new SimpleDateFormat("yyyy/MM/dd");
        long l = Long.valueOf(time);
        String times = sf.format(new Date(l));//单位秒
        return times;
    }

    /**
     * 时间戳转换string
     *
     * @param time
     * @return
     */
    public static String getStringTimeStr(String time) {
        sf = new SimpleDateFormat("yyyy.MM.dd");
        long l = Long.valueOf(time);
        String times = sf.format(new Date(l));//单位秒
        return times;
    }


    public static String getDateToString(long time) {
        Date d = new Date(Long.parseLong(time + ""));
        sf = new SimpleDateFormat("yyyy/MM/dd");
        return sf.format(d);
    }

    public static String getDateToString(Date time) {
        //Date d = new Date(Long.parseLong(time+"000"));
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(time);
    }

    public static String getDateTojob(Date time) {
        //Date d = new Date(Long.parseLong(time+"000"));
        sf = new SimpleDateFormat("yyyy.MM.dd");
        return sf.format(time);
    }

    public static String getDateToStringSim(long time) {
        Date d = new Date(Long.parseLong(time + ""));
        sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return sf.format(d);
    }

    public static String getDateToStringSimple(long time) {
        Date d = new Date(Long.parseLong(time + ""));
        sf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        return sf.format(d);
    }

    public static String getDateToStringorder(long time) {
        Date d = new Date(Long.parseLong(time + ""));
        sf = new SimpleDateFormat("yyyy-MM-dd");
        return sf.format(d);
    }

    public static String getDateToStringbill(long time) {
        Date d = new Date(Long.parseLong(time + ""));
        sf = new SimpleDateFormat("MM-dd HH:mm");
        return sf.format(d);
    }

    public static String getDateToStringbuss(long time) {
        Date d = new Date(Long.parseLong(time + ""));
        sf = new SimpleDateFormat("HH:mm");
        return sf.format(d);
    }

    public static long getStringToDate(String time) {
        sf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分");
        Date date = new Date();
        try {
            date = sf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }

    public static long getStringToDates(String time) {
        sf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date = sf.parse(time);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date.getTime();
    }


//    public static Date getStringToDate(String time) {
//        sf = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
//        Date date = new Date();
//        try {
//            date = sf.parse(time);
//        } catch (ParseException e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        return date;
//    }


    /**
     * 计算当前时间与过去时间差
     *
     * @param dateStr 过去时间
     * @return
     */
    public static String getDateDiff(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        Date currentDate = new Date();
        Date date = null;
        try {
            date = sdf.parse(dateStr);

        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        long past = currentDate.getTime() - date.getTime();
        long day = past / (24 * 60 * 60 * 1000);
        long hour = (past / (60 * 60 * 1000) - day * 24);
        long min = ((past / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (past / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String time = day + "D/" + hour + "H/" + min + "M/" + s + "S/";
        return time;
    }


    public static String getDateDiff_min(String new_time,String old_time) {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

//        Date currentDate = new Date();
        Date new_data = null;
        Date old_data = null;
        try {
            new_data = sdf.parse(new_time);
            old_data = sdf.parse(old_time);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
        long past = new_data.getTime() - old_data.getTime();
        long day = past / (24 * 60 * 60 * 1000);
        long hour = (past / (60 * 60 * 1000) - day * 24);
        long min = ((past / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (past / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String time = day + "D/" + hour + "H/" + min + "M/" + s + "S/";
        if (day>0){

            day=day*24;
        }
        if (hour>0){

            if (day>0){
                hour=day+hour;
            }
        }else {
            if (day>0){
                hour=day+hour;
            }
        }

        if (min>0){



            /*
            * 判断 天，小时，是否大于0 如果大于0 就获取多少小时×60分，+分钟数
            *
            * */


            long min2=hour*60;


            min=min2+min;
        }

        return String.valueOf(min);
    }

    public static boolean getDateDiff15(String dateStr) {
        boolean isold = false;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDate = new Date();
        Date date = null;
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            return isold;
        }
        long past = currentDate.getTime() - date.getTime();
        long day = past / (24 * 60 * 60 * 1000);
        long hour = (past / (60 * 60 * 1000) - day * 24);
        long min = ((past / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (past / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        String time = day + "D/" + hour + "H/" + min + "M/" + s + "S/";
        if (day > 15) {
            isold = true;
        } else {
            isold = false;
        }
        return isold;
    }

    public static String getStr(Long time) {
        StringBuffer sb = new StringBuffer();
        long between = 0;
        try {
            Date begin = new Date(time);
            Date end = new Date(System.currentTimeMillis());
            between = (end.getTime() - begin.getTime());// 得到两者的毫秒数
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        long day = between / (24 * 60 * 60 * 1000);
        long hour = (between / (60 * 60 * 1000) - day * 24);
        long min = ((between / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (between / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long ms = (between - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000
                - min * 60 * 1000 - s * 1000);
        if (day > 0) {
            sb.append(day + "天");
        } else if (hour > 0) {
            sb.append(hour + "小时");
        } else if (min > 0) {
            sb.append(min + "分钟");
        } else if (s > 0) {
            sb.append(s + "秒");
        } else {
            sb.append("刚刚");
        }
        if (!sb.toString().equals("刚刚")) {
            sb.append("前");
        }

        return sb.toString();
    }

    /**
     *   * 日期格式转换yyyy-MM-dd'T'HH:mm:ss.SSSXXX  (yyyy-MM-dd'T'HH:mm:ss.SSSZ) TO  yyyy-MM-dd HH:mm:ss
     *   * @throws ParseException 
     *  
     */
    public static String dealDateFormat(String oldDateStr) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX"); //yyyy-MM-dd'T'HH:mm:ss.SSSZ
        Date date = df.parse(oldDateStr);
        SimpleDateFormat df1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
        Date date1 = df1.parse(date.toString());
        DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//  Date date3 =  df2.parse(date1.toString());
        return df2.format(date1);
    }


    public static String dealDateFormat2(String oldDateStr) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //yyyy-MM-dd'T'HH:mm:ss.SSSZ
        Date date = df.parse(oldDateStr);
        DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
        return df2.format(date);
    }

}